package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.pojo.vo.comPriorityVO;
import com.cqut.service.ICompetitionManagerService;
import com.cqut.dao.CompetitionManagerMapper;
import com.cqut.dao.CompetitionManagerMapperExtra;
import com.cqut.dto.ComPriorityDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.model.CompetitionManager;
import com.cqut.util.SqlUtil;

@Service
public class CompetitionManagerService implements ICompetitionManagerService {
    @Resource(name="competitionManagerMapper")
    private CompetitionManagerMapper competitionManagerMapper;
    
    @Resource(name="competitionManagerMapperExtra")
    private CompetitionManagerMapperExtra competitionManagerMapperExtra;

    @Override
    public boolean save(CompetitionManager competitionManager) {
    	String competitionId = String.valueOf(System.currentTimeMillis());
    	int state = 0;
    	Double browseVolume = (double) 0;
    	competitionManager.setCompetitionId(competitionId);
    	competitionManager.setState(state);
    	competitionManager.setBrowseVolume(browseVolume);
    	String hostUnitId = String.valueOf(System.currentTimeMillis());
    	competitionManagerMapperExtra.insertHost(competitionManager.getAssistUnitName(),competitionManager.getCompetitionId(),competitionManager.getHostUnitContact(),competitionManager.getHostUnitContactMail(),competitionManager.getHostUnitContactPerson(),competitionManager.getHostUnitName(),hostUnitId);
        return competitionManagerMapper.insert(competitionManager) > 0;
    }

    @Override
    public boolean update(CompetitionManager competitionManager) {
        return competitionManagerMapper.updateByPrimaryKeySelective(competitionManager) >= 0;
    }

    @Override
    public boolean delete(String competitionId) {
        return competitionManagerMapper.deleteByPrimaryKey(competitionId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return competitionManagerMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public CompetitionManager findModel(String competitionId) {
        return competitionManagerMapper.selectByPrimaryKey(competitionId);
    }

    @Override
    public CompetitionManager findModel(String competitionId, String[] parameters) {
        return competitionManagerMapper.findModel(competitionId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String competitionId) {
        String[][] parameters = {{"competition_id", "competitionId"}, {"competition_name", "competitionName"}, {"sign_up_start_time", "signUpStartTime"}, {"sign_up_end_time", "signUpEndTime"}, {"is_multi_stage", "isMultiStage"}, {"propaganda_path", "propagandaPath"}, {"competition_type", "competitionType"}, {"state", "state"}, {"check_user_name", "checkUserName"}, {"check_time", "checkTime"}, {"check_remark", "checkRemark"}, {"competition_start_time", "competitionStartTime"}, {"competition_end_time", "competitionEndTime"}, {"competition_image", "competitionImage"}, {"competition_movie", "competitionMovie"}, {"competition_remark", "competitionRemark"}, {"enclosure", "enclosure"}, {"browse_volume", "browseVolume"}, {"level", "level"}, {"game_type", "gameType"}, {"least_number", "leastNumber"}, {"max_number", "maxNumber"}, {"priority", "priority"}};
        return this.findMap(competitionId, parameters);
    }

    public Map<String, Object> findMap(String competitionId, String[][] parameters) {
        return competitionManagerMapper.findMap(competitionId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<CompetitionManager> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<CompetitionManager> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<CompetitionManager> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return competitionManagerMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"competition_id", "competitionId"}, {"competition_name", "competitionName"}, {"sign_up_start_time", "signUpStartTime"}, {"sign_up_end_time", "signUpEndTime"}, {"is_multi_stage", "isMultiStage"}, {"propaganda_path", "propagandaPath"}, {"competition_type", "competitionType"}, {"state", "state"}, {"check_user_name", "checkUserName"}, {"check_time", "checkTime"}, {"check_remark", "checkRemark"}, {"competition_start_time", "competitionStartTime"}, {"competition_end_time", "competitionEndTime"}, {"competition_image", "competitionImage"}, {"competition_movie", "competitionMovie"}, {"competition_remark", "competitionRemark"}, {"enclosure", "enclosure"}, {"browse_volume", "browseVolume"}, {"level", "level"}, {"game_type", "gameType"}, {"least_number", "leastNumber"}, {"max_number", "maxNumber"}, {"priority", "priority"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"competition_id", "competitionId"}, {"competition_name", "competitionName"}, {"sign_up_start_time", "signUpStartTime"}, {"sign_up_end_time", "signUpEndTime"}, {"is_multi_stage", "isMultiStage"}, {"propaganda_path", "propagandaPath"}, {"competition_type", "competitionType"}, {"state", "state"}, {"check_user_name", "checkUserName"}, {"check_time", "checkTime"}, {"check_remark", "checkRemark"}, {"competition_start_time", "competitionStartTime"}, {"competition_end_time", "competitionEndTime"}, {"competition_image", "competitionImage"}, {"competition_movie", "competitionMovie"}, {"competition_remark", "competitionRemark"}, {"enclosure", "enclosure"}, {"browse_volume", "browseVolume"}, {"level", "level"}, {"game_type", "gameType"}, {"least_number", "leastNumber"}, {"max_number", "maxNumber"}, {"priority", "priority"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return competitionManagerMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return competitionManagerMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

    @Override
	public List<comPriorityVO> loadComPriorityList(
			ComPriorityDTO comPriorityDTO, LimitShowDTO limitShowDTO) {
		return competitionManagerMapperExtra.findComPriority(comPriorityDTO.getCompetitionName(),comPriorityDTO.getOperatorId(),limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}

	@Override
	public Object findCountOfCompetitions(ComPriorityDTO comPriorityDTO) {
		String count = 
				competitionManagerMapperExtra.findComPriorityNo(comPriorityDTO.getCompetitionName(),comPriorityDTO.getOperatorId(), -1, -1);
		return count;
	}

	/**
	 * 获得list 
	 * **/
	public List<Map<String, Object>> getComParamList(String competitionName,LimitShowDTO limitShowDTO){
		return competitionManagerMapperExtra.getComParamListByName(competitionName,limitShowDTO.calculateStart(),limitShowDTO.calculateEnd());
	}
	
	/**
	 * 获得count
	 * **/
	public long getComParamListCount(String competitionName){
		return competitionManagerMapperExtra.getComParamListCountByName(competitionName);
	}
	
	/**
	 * 获得list 
	 * **/
	public List<Map<String, Object>> getComParamPassList(String competitionName,LimitShowDTO limitShowDTO){
		return competitionManagerMapperExtra.getComParamPassListByName(competitionName,limitShowDTO.calculateStart(),limitShowDTO.calculateEnd());
	}
	
	/**
	 * 获得count
	 * **/
	public long getComParamPassListCount(String competitionName){
		return competitionManagerMapperExtra.getComParamPassListCountByName(competitionName);
	}
	
	/**
	 * 根据competitionId获得数据
	 * **/
	public Map<String, Object> getComDetailBycompetitionId1(String competitionId){
		return competitionManagerMapperExtra.getComDetailBycompetitionId1(competitionId);
	}
	public Map<String, Object> getComDetailBycompetitionId2(String competitionId){
		return competitionManagerMapperExtra.getComDetailBycompetitionId2(competitionId);
	}
	
	/**
	 * 根据大赛ID获得竞赛推荐详情
	 */
	public Map<String, Object> getComRecommendDetailById(String competitionId){
		return competitionManagerMapperExtra.getComDetailBycompetitionId(competitionId);
	}
	
	
	/**
	 * 根据priority上移
	 * **/
	public boolean shiftUpByPriority(int priority){
		String up = competitionManagerMapperExtra.findIdByPriority(priority);
		String down = competitionManagerMapperExtra.findIdByPriority(priority-1);
		if(competitionManagerMapperExtra.shiftByPriority(priority-1,up)>0&&competitionManagerMapperExtra.shiftByPriority(priority,down)>0)
			return true;
		else 
			return false;
	}
	
	/**
	 * 根据priority下移
	 * **/
	public boolean shiftDownByPriority(int priority){
		String down = competitionManagerMapperExtra.findIdByPriority(priority);
		String up = competitionManagerMapperExtra.findIdByPriority(priority+1);	
		if(competitionManagerMapperExtra.shiftByPriority(priority,up)>0&&competitionManagerMapperExtra.shiftByPriority(priority+1,down)>0)
			return true;
		else 
			return false;
	}
	
	/**
	 * 根据priority置顶
	 * **/
	public boolean shiftTopByPriority(int priority){
		String top = competitionManagerMapperExtra.findIdByPriority(priority);
		for(int i=priority-1;i>0;i--){
			String down = competitionManagerMapperExtra.findIdByPriority(i);
			competitionManagerMapperExtra.shiftByPriority(i+1,down);
		}
		if(competitionManagerMapperExtra.shiftByPriority(1,top)>0)
			return true;
		else return false;
	}

	/**
	 * 改变状态值
	 * */
	@Override
	public boolean updateStateToProgress(String competitionId, String state) {
		if(competitionManagerMapperExtra.updateStateToProgress(competitionId, state) > 0)
			return true;
		else return false;
	}

	@Override
	public boolean updateStateToStorage(String competitionId, String state) {
		if(competitionManagerMapperExtra.updateStateToStorage(competitionId, state) > 0)
			return true;
		else return false;
	}
	
	@Override
	public Object findNumOfFollow(String competitionId) {
		String count = 
				competitionManagerMapperExtra.findComFollowNum(competitionId);
		return count;
	}

}
